﻿<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>角色管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="/plugins/layuiadmin/style/admin.css" media="all">
</head>
<body>
  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-card-body">
        <div style="padding-bottom: 10px;">
          <button class="layui-btn layui-btn-danger layuiadmin-btn-list" data-type="batchdel"><i class="layui-icon layui-icon-delete"></i>删除</button>
          <button class="layui-btn layui-btn-normal layuiadmin-btn-list" data-type="add"><i class="layui-icon layui-icon-add-1"></i>添加</button>
          <button class="layui-btn layuiadmin-btn-list" data-type="refresh"><i class="layui-icon layui-icon-refresh"></i>刷新</button>
          <div class="layui-form" lay-filter="dds-search-form" id="dds-search-form" style="float:right">
            <div class="layui-inline">
             <input class="layui-input" name="name" id="name" placeholder="请输入搜索条件">
            </div>
            <button class="layui-btn" lay-submit lay-filter="btn_search"><i class="layui-icon layui-icon-search"></i>搜索</button>
          </div>
        </div>
        <table id="dds-list" lay-filter="dds-list"></table> 
        <script type="text/html" id="tpl_isadmin">
          {{#  if(d.isadmin==1){ }}
            <button class="layui-btn layui-btn-xs">是</button>
          {{#  } else { }}
            <button class="layui-btn layui-btn-primary layui-btn-xs">否</button>
          {{#  } }}
        </script>
        <script type="text/html" id="table-content-list">
          <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
          <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="departs"><i class="layui-icon layui-icon-edit"></i>部门权限</a>
          <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="menus"><i class="layui-icon layui-icon-edit"></i>菜单权限</a>
          <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
        </script>
      </div>
    </div>
  </div>
  <script src="/plugins/layui/layui.js"></script> 
  <script>
  layui.config({
    base: '/plugins/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index','table'], function(){
    var $ = layui.$,table = layui.table,form = layui.form;

    table.render({
      elem: '#dds-list'
      , url: 'role_list' //接口
      , cols: [[
        { type: 'checkbox', fixed: 'left' }
        , { field: 'name', title: '角色名称', minWidth: 100 }
        , { field: 'code', title: '角色标识', minWidth: 100 }
        , { field: 'detail', title: '角色描述', minWidth: 100 }
        , { field: 'isadmin', title: '是否是管理员',width:150,align: 'center', templet: '#tpl_isadmin' }
        , { title: '操作', minWidth: 350, align: 'center', fixed: 'right', toolbar: '#table-content-list' }
        ]]
        , page: false
      , text: {
        none: '暂无相关数据'
      }
    });

    //监听搜索
    form.on('submit(btn_search)', function(data){
      var field = data.field;
      //执行重载
      table.reload('dds-list', {
        where: field
      });
    });

    //监听操作
    var active = {
      batchdel: function(){
        var checkStatus = table.checkStatus('dds-list')
        ,checkData = checkStatus.data; //得到选中的数据
        if(checkData.length === 0){
          return layer.msg('请选择数据');
        }

        var ids = [];//存储id的数组
        checkData.forEach(function (item) {
          ids.push(item.id);
        });
      
        layer.confirm('您确定要删除吗？删除后将无法进行恢复。',{icon: 3, title:'提示'}, function (index) {
          $.ajax({
                type: "post",
                url: "role_list",
                data: {ids:ids.join(",")},
                dataType: "json",
                success: function (data) {
                    if (data.code == 0)
                    {
                      table.reload('dds-list');
                      layer.msg(data.msg);
                    }
                }
          });
        });
      },
      add: function(){
        layer.open({
          type: 2
          ,title: '添加角色'
          ,content: 'role_add.html'
          ,maxmin: true
          ,area: ['500px', '450px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            //点击确认触发 iframe 内容中的按钮提交
            var submit = layero.find('iframe').contents().find("#btn_submit");
            submit.click();
          }
        }); 
      },
      refresh: function () {
        table.reload('dds-list');
      }
    }; 

    $('.layui-btn.layuiadmin-btn-list').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

    //监听工具条
    table.on('tool(dds-list)', function(obj){
      var data = obj.data;
      if(obj.event === 'del'){
        layer.confirm('您确定要删除吗？删除后将无法进行恢复。',{icon: 3, title:'提示'}, function(index){
          $.ajax({
                type: "post",
                url: "role_list",
                data: {ids:data.id},
                dataType: "json",
                success: function (data) {
                    if (data.code == 0)
                    {
                      table.reload('dds-list');
                      layer.msg(data.msg);
                    }
                }
          });
        });
      } else if(obj.event === 'edit'){
        layer.open({
          type: 2
          ,title: '编辑角色'
          ,content: 'role_add.html#/id='+data.id
          ,maxmin: true
          ,area: ['500px', '450px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var submit = layero.find('iframe').contents().find("#btn_submit");
            submit.trigger('click');
            }
        });
      } else if(obj.event === 'departs'){
        layer.open({
          type: 2
          ,title: '部门权限'
          ,content: 'role_departs.html#/roleid='+data.id
          ,maxmin: true
          ,area: ['600px', '550px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var submit = layero.find('iframe').contents().find("#btn_submit");
            submit.trigger('click');
            }
        });
      } else if(obj.event === 'menus'){
        layer.open({
          type: 2
          ,title: '菜单权限'
          ,content: 'role_menus.html#/roleid='+data.id
          ,maxmin: true
          ,area: ['600px', '550px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var submit = layero.find('iframe').contents().find("#btn_submit");
            submit.trigger('click');
            }
        });
      } 
    });

  });
  </script>
</body>
</html>
